package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.ICustomerService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.service.ICaseSysService;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统公用_工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-03
 */
@Service
public class CaseSysServiceImpl implements ICaseSysService 
{
    @Autowired
    private GuaranteeCaseMapper caseSysMapper;
    @Autowired
     private EmployeeMapper employeeMapper;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private CaseComplaintMapper caseComplaintMapper;
    @Autowired
    private CaseTypeMapper caseTypeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private PositionMapper positionMapper;

//    /**
//     * 查询系统公用_工单
//     *
//     * @param caseId 系统公用_工单ID
//     * @return 系统公用_工单
//     */
//    @Override
//    public CaseSys selectCaseSysById(Long caseId)
//    {
//        return caseSysMapper.selectCaseSysById(caseId);
//    }
//
//    /**
//     * 查询系统公用_工单列表
//     *
//     * @param caseSys 系统公用_工单
//     * @return 系统公用_工单
//     */
//    @Override
//    public List<CaseSys> selectCaseSysList(CaseSys caseSys)
//    {
//        return caseSysMapper.selectCaseSysList(caseSys);
//    }
//
//    /**
//     * 新增系统公用_工单
//     *
//     * @param caseSys 系统公用_工单
//     * @return 结果
//     */
//    @Override
//    public int insertCaseSys(CaseSys caseSys)
//    {
//        return caseSysMapper.insertCaseSys(caseSys);
//    }
//
//    /**
//     * 修改系统公用_工单
//     *
//     * @param caseSys 系统公用_工单
//     * @return 结果
//     */
//    @Override
//    public int updateCaseSys(CaseSys caseSys)
//    {
//        return caseSysMapper.updateCaseSys(caseSys);
//    }
//
//    /**
//     * 批量删除系统公用_工单
//     *
//     * @param caseIds 需要删除的系统公用_工单ID
//     * @return 结果
//     */
//    @Override
//    public int deleteCaseSysByIds(Long[] caseIds)
//    {
//        return caseSysMapper.deleteCaseSysByIds(caseIds);
//    }
//
//    /**
//     * 删除系统公用_工单信息
//     *
//     * @param caseId 系统公用_工单ID
//     * @return 结果
//     */
//    @Override
//    public int deleteCaseSysById(Long caseId)
//    {
//        return caseSysMapper.deleteCaseSysById(caseId);
//    }
    /**
     * 我的任务工单查询
     * @param caseMIne
     * @return
     */
    @Override
    public List<TmpCaseMIneVo> selectCaseMine(CaseMIne caseMIne, HttpServletRequest request) throws Exception {
        Employee loginEmployee = loginService.getLoginEmployee(request);
        if (loginEmployee == null){
            throw new Exception("登录已过期");
        }
        caseMIne.setId(loginEmployee.getEmployeeId().toString());
        if (caseMIne.getEndTime() != null && !caseMIne.getEndTime().equals(""))
            caseMIne.setEndTime(caseMIne.getEndTime() + " 23:59:59");
        List<TmpCaseMIneVo> caseMIneVos = caseSysMapper.selectCaseMine(caseMIne);
        if (caseMIneVos.size() > 0){
            for (TmpCaseMIneVo caseMIneVo : caseMIneVos) {
                if (caseMIneVo.getCaseTypeId() != null){
                    CaseType caseType = caseTypeMapper.selectCaseTypeById(Long.valueOf(caseMIneVo.getCaseTypeId()));
                    if (caseType != null){
                        caseMIneVo.setCaseTypeName(caseType.getCaseTypeName());
                    }
                }
                Employee employee = employeeMapper.backSelectEmployee(caseMIneVo.getCreateEmployeeId());
                if (employee != null){
                    caseMIneVo.setEmployeeName(employee.getEmployeeName());
                    caseMIneVo.setOperator(employee.getEmployeeName());
                }

                Department department = departmentMapper.selectDepartmentById(caseMIneVo.getDeptId());
                if (department != null){
                    caseMIneVo.setDeptName(department.getDeptName());
                }

                if (caseMIneVo.getCustomerId() != null){
                    CustomerIdVo customerIdVo = new CustomerIdVo();
                    customerIdVo.setCustomerId(caseMIneVo.getCustomerId());
                    customerIdVo = customerService.customerId(customerIdVo);
                    if (customerIdVo != null){
                        caseMIneVo.setCustomerName(customerIdVo.getCustomerName());
                        caseMIneVo.setPhone(customerIdVo.getCustomerPhone());
                    }
                }

                if (caseMIneVo.getReceivingDepartment() != null && !caseMIneVo.getReceivingDepartment().equals("")) {
                    Department departmentById = departmentMapper.selectDepartmentById(Long.valueOf(caseMIneVo.getReceivingDepartment()));
                    if (departmentById != null){
                        caseMIneVo.setJsDeptName(departmentById.getDeptName());
                    }
                }

                PositionDTO positionDTO = null;
                if (caseMIneVo.getReceivePost() != null){
                    positionDTO = positionMapper.selectPositionById(Long.valueOf(caseMIneVo.getReceivePost()));
                }

                if (positionDTO != null){
                    caseMIneVo.setJsPosition(positionDTO.getPositionName());
                }

                if (caseMIneVo.getReceiver() != null && !caseMIneVo.getReceiver().equals("")) {
                    Employee selectEmployee = employeeMapper.backSelectEmployee(Long.valueOf(caseMIneVo.getReceiver()));
                    if (selectEmployee != null){
                        caseMIneVo.setJsPerson(selectEmployee.getEmployeeName());
                    }
                }

                if (caseMIneVo.getPri() != null){
                    if (caseMIneVo.getPri().equals("0")){
                        caseMIneVo.setPri("普通");
                    }
                    else if (caseMIneVo.getPri().equals("1")){
                        caseMIneVo.setPri("紧急");
                    }
                }

                if (caseMIneVo.getCaseStatus() != null){
                    if (caseMIneVo.getCaseStatus().equals("0")){
                        caseMIneVo.setCaseStatus("待处理");
                    }
                    else if (caseMIneVo.getCaseStatus().equals("1")){
                        caseMIneVo.setCaseStatus("处理完");
                    }
                    else if (caseMIneVo.getCaseStatus().equals("2")){
                        caseMIneVo.setCaseStatus("废弃");
                    }
                }
                if (caseMIneVo.getCaseClaimId()!=null)
                {
                    caseMIneVo.setCaseType("claim");
                    caseMIneVo.setCaseClaimId(caseMIneVo.getCaseClaimId());
                }
                if (caseMIneVo.getCaseComplaintId()!=null)
                {
                    caseMIneVo.setCaseType("complaint");
                    caseMIneVo.setCaseComplaintId(caseMIneVo.getCaseComplaintId());
                }
                if (caseMIneVo.getCaseMalfeasanceId()!=null)
                {
                    caseMIneVo.setCaseType("derelictionDuty");
                    caseMIneVo.setCaseMalfeasanceId(caseMIneVo.getCaseMalfeasanceId());
                }
                if (caseMIneVo.getCaseClaimId()==null && caseMIneVo.getCaseComplaintId()==null && caseMIneVo.getCaseMalfeasanceId()==null)
                {
                    caseMIneVo.setCaseType("ordinary");
                }
            }
        }
        return caseMIneVos;
    }

    /**
     * 工单转移
     * */
    @Override
    public List<TransferCountVo> transferCount(HttpServletRequest request, TransferVo transferVo) throws Exception {
        List<TransferCountVo> transferCountVos = new ArrayList<>();
        Employee loginEmployee = loginService.getLoginEmployee(request);
        if (loginEmployee == null){
            throw new Exception("登录已过期");
        }

        for (Long id : transferVo.getIds()) {
            TransferCountVo transferCountVo = new TransferCountVo();
            transferCountVo.setId(id);
            GuaranteeCase guaranteeCase = caseSysMapper.guaranteeCase(id);
            if (guaranteeCase == null){
                transferCountVo.setMsg("无效的工单id");
            }

            if (guaranteeCase.getTransferCount() == null || guaranteeCase.getTransferCount() < 2){
                if (transferVo.getEmployeeId() != null){
                    guaranteeCase.setReceiver(transferVo.getEmployeeId().toString());
                    int i = caseSysMapper.updateGuaranteeCase(guaranteeCase);
                    if (i > 0){
                        if (guaranteeCase.getTransferCount() != null){
                            guaranteeCase.setTransferCount(guaranteeCase.getTransferCount() + 1);
                        }
                        else {
                            guaranteeCase.setTransferCount(1l);
                        }
                        caseSysMapper.updateGuaranteeCase(guaranteeCase);
                        transferCountVo.setMsg("转移成功");
                    }
                    else {
                        transferCountVo.setMsg("转移失败");
                    }
                }
                else {
                    transferCountVo.setMsg("未找到转移目标");
                }
            }
            else {
                transferCountVo.setMsg("工单转移次数已达到上限");
            }
            transferCountVos.add(transferCountVo);
        }
        return transferCountVos;
    }
}
